<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class marketRecoverUserDiamond extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'marketRecoverUserDiamond';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '营销业务回收长期未登录的用户钻石';

    protected $errorLog = 'error_adminrecover_diamond';//后台回收钻石失败记录
    protected $log = 'adminrecover_diamond';//脚本回收钻石记录
    protected $connection = 'api';
    protected $finance_wallet_table = 'finance_wallet';//用户钱包
    protected $finance_recharge_order_table = 'finance_recharge_order';//充值记录表

    protected $apiservice;//调用api服务
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $fiveUserIds = [
            '19301','18135','14919','19526','17113','13144','20175','16599',
            '15673','10361','13309','19430','11362','19240','10059','11736',
            '14203','13294','10074'
        ];
        $tenUserIds = [
            '14567','15026','10043','11217','13158','11518','10436','12843',
            '13399','16219','14840','14123','16288','13535'
        ];
        //500钻石发送
        foreach($fiveUserIds as $v)
        {
            $this->decrease($v,500);
        }
        //1000钻石发送
        foreach($tenUserIds as $v)
        {
            $this->decrease($v,1000);
        }
    }
    /**
     * 减去钻石
     */
    public function decrease($userid,$diamonvalue){
        //查看用户是否充值过若没有充值过则扣除送的钻(前提账户还有钻石的话)
        $rechargeOrder = DB::connection($this->connection)->table($this->finance_recharge_order_table)->where(['user_id'=>$userid,'status'=>1])->where('pay_timestamp','>=',1692633600)->get();
        $rechargeOrder = objToArray($rechargeOrder);
        write_log($this->log,'INFO 回收钻用户:'.$userid.'充值状态',$rechargeOrder);
        //若为空的话减去钻石
        try{
            if(empty($rechargeOrder)){
                //查看用户账户是否有钻石没有的话不做处理
                $userDiamonInfo = DB::connection($this->connection)->table($this->finance_wallet_table)->where(['user_id'=>$userid])->first();
                $userDiamonInfo = objToArray($userDiamonInfo);
                write_log($this->log,'INFO 用户:'.$userid.'钱包状态',$userDiamonInfo);
                if(!empty($userDiamonInfo) && $userDiamonInfo['diamond_balance'] >=$diamonvalue)
                {
                    $requestData = [
                        'user_id' => $userid,
                        'type' => 4,//4.后台钻石-加减
                        'amount' => $diamonvalue,
                        'description' => '活动结束回收钻石',
                    ];
                    $res = curl_request(getKkaudioApiUrl() . '/inner/finance/decrease', $requestData, 'urlencoded', 'POST');
                    write_log($this->log,'INFO 回收钻用户:'.$userid.'状态',$res);
                }
            }
        }catch (\Exception $e) {
                write_log($this->errorLog,'FAIL 回收钻石失败用户:',$userid);
        }
    }
}
